package com.cy.aircompressor.main.home.lifecycle;

import android.os.Bundle;

import androidx.fragment.app.FragmentManager;

import com.cy.aircompressor.R;
import com.cy.aircompressor.databinding.DialogLifecycleLayoutBinding;
import com.cy.aircompressor.main.home.lifecycle.child.AirCompressorManagerFragment;
import com.cy.aircompressor.main.home.lifecycle.child.ExecutiveMaintenanceFragment;
import com.cy.aircompressor.main.home.lifecycle.child.PartsManagerFragment;
import com.cy.aircompressor.main.home.lifecycle.child.SensorManagerFragment;
import com.cy.aircompressor.main.view.HeaderBar;
import com.cy.aircompressor.main.view.HeaderBarTab;
import com.drug.lib.util.DensityUtil;
import com.jakewharton.rxbinding2.view.RxView;
import com.net.model.base.fragment.BaseVBDialogFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import me.yokeyword.fragmentation.SupportFragment;

public class LifeCycleDialogFragment extends BaseVBDialogFragment<DialogLifecycleLayoutBinding> {
    String compressorId;

    public static LifeCycleDialogFragment showDialogFragment(FragmentManager fragmentManager, String tag, String compressorId) {
        Bundle args = new Bundle();
        LifeCycleDialogFragment fragment = new LifeCycleDialogFragment();
        args.putString("compressorId", compressorId);
        fragment.setArguments(args);
        fragment.show(fragmentManager, tag);
        return fragment;
    }

    @Override
    protected DialogLifecycleLayoutBinding getViewBinding() {
        return DialogLifecycleLayoutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        compressorId = getArguments().getString("compressorId");
        initFragments();
        initView();
    }

    @Override
    protected void requestNetwork() {
    }


    @Override
    protected int getWindowHeight() {
        return (getScreenHeight() - DensityUtil.dip2px(78));
    }

    @Override
    protected int getWindowWidth() {
        return (super.getScreenWidth() - DensityUtil.dip2px(106));
    }

    @Override
    protected void initialize() {

    }



    private List<SupportFragment> fragments = new ArrayList<>();

    private void initFragments() {
        SupportFragment firstFragment = findFragment(ExecutiveMaintenanceFragment.class);
        if (firstFragment != null) {
            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题
            // 这里我们需要拿到mFragments的引用
            fragments.add(firstFragment);
            fragments.add(findFragment(AirCompressorManagerFragment.class));
            fragments.add(findFragment(SensorManagerFragment.class));
            fragments.add(findFragment(PartsManagerFragment.class));
        } else {
            fragments.add(ExecutiveMaintenanceFragment.newInstance());
            fragments.add(AirCompressorManagerFragment.newInstance());
            fragments.add(SensorManagerFragment.newInstance());
            fragments.add(PartsManagerFragment.newInstance());
            loadMultipleRootFragment(R.id.fl_container, 0,
                    fragments.get(0),
                    fragments.get(1),
                    fragments.get(2),
                    fragments.get(3));
        }
    }

    private void initView() {
        mBinding.headerBar
                .addItem(new HeaderBarTab(mContext, getString(R.string.nav_zxwb)))
                .addItem(new HeaderBarTab(mContext, getString(R.string.nav_kyjgl)))
                .addItem(new HeaderBarTab(mContext, getString(R.string.nav_cgqgl)))
                .addItem(new HeaderBarTab(mContext, getString(R.string.nav_lbjgl)));
        mBinding.headerBar.setOnTabSelectedListener(new HeaderBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                showHideFragment(fragments.get(position), fragments.get(prePosition));
            }

            @Override
            public void onTabUnselected(int position) {
            }

            @Override
            public void onTabReselected(int position) {
            }
        });

        RxView.clicks(mBinding.closeBtn)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    dismiss();
                });
    }
}
